<template>
    <div>
        <Row>
            <Button @click="saveNewCarInfo" style="float: right" type="success" v-if="isCheck">保存</Button>
        </Row>
        <Form ref="carInfo" :label-width="160" :model="carInfo" label-position="right" inline >
            <Row>
                <Col span="8">
                    <FormItem prop="brandName" label="品牌">
                        <Input type="text" v-model="carInfo.brandName" disabled placeholder="请输入">
                        </Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="modelName" label="车型">
                        <Poptip placement="bottom" v-if="carInfo.backSign==='yes'&&carInfo.modelName!==carInfo.modelNameOld" >
                            <Input type="text" v-model="carInfo.modelName" disabled placeholder="请输入" icon="ios-warning">
                            </Input>
                            <div slot="title" style="font-weight: 700;width: 175px">{{carInfo.modelNameOld}}</div>
                        </Poptip>
                        <Input v-if="carInfo.backSign!=='yes'||carInfo.modelName===carInfo.modelNameOld" type="text" v-model="carInfo.modelName" disabled placeholder="请输入" style="width: 175px">
                        </Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="guidePrice" label="新车指导价">
                        <Input type="text" v-model="carInfo.guidePrice" disabled placeholder="请输入">
                        </Input>
                    </FormItem>
                </Col>
            </Row>
            <Row>
                <Col span="16">
                    <FormItem prop="styleName" label="款式">
                        <div style="display: flex; flex-flow: row;">
                        <Poptip placement="bottom" v-if="carInfo.backSign==='yes'&&carInfo.styleName!==carInfo.styleNameOld" >
                            <Input type="text" v-model="carInfo.styleName" disabled placeholder="请输入" icon="ios-warning">
                            </Input>
                            <div slot="title" style="font-weight: 700">{{carInfo.styleNameOld}}</div>
                        </Poptip>
                        <Input v-if="carInfo.backSign!=='yes'||carInfo.styleName===carInfo.styleNameOld" type="text" style="width: 590px" v-model="carInfo.styleName" disabled placeholder="请输入">
                        </Input>
                        <Button @click="showCarStyleDetailInfo" ghost type="success" style="margin-left: 5px">查看配置</Button>
                        </div>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="salePrice" label="车辆销售价格" >
                        <Poptip placement="right" v-model="carInfo.warningInfo.car.showflag" >
                            <Input type="text" v-model="carInfo.salePrice" disabled placeholder="请输入" icon="ios-warning">
                            </Input>
                            <div slot="title" style="font-weight: 700">{{carInfo.warningInfo.car.title}}</div>
                            <div slot="content">
                                <Row>
                                    <iframe :src=carInfo.warningInfo.car.msgs frameborder="0" scrolling="auto"></iframe>
                                </Row>
                            </div>
                        </Poptip>
                    </FormItem>
                </Col>
            </Row>
            <Row>
                <Col span="8">
                    <FormItem prop="purchaseAddress" label="购车地点">
                        <Input type="text" v-model="carInfo.purchaseAddress" disabled placeholder="请输入">
                        </Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="carBodyClass" label="车身类型">
                        <Select v-model="carInfo.carBodyClass" disabled placeholder="请选择">
                            <Option v-for="item in dataDic.carBodyClass" :label="item.title" :value="item.value" :key="item.value">{{ item.title }}</Option>
                        </Select>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="firmPrice" label="认定价格" >
                        <Input type="number" v-model="carInfo.firmPrice" placeholder="请输入" :disabled="!isCheck">
                        </Input>
                    </FormItem>
                </Col>
            </Row>
            <Row>
                <Col span="8">
                    <FormItem prop="plateAddress" label="上牌地点">
                        <!--<Input type="text" v-model="carInfo.plateAddress" disabled placeholder="请输入">
                        </Input>-->
                        <i-select style="width: 150px" v-model="carInfo.licenseProvince" placement="top" placeholder="请选择" @on-change="queryCity" :disabled="!isCheck">
                            <i-option v-for="(item,index) in provinces" style="width: 150px" :label="item.label" :value="item.value" :key="index">{{ item.label }}</i-option>
                        </i-select>
                        <Select style="width: 150px" v-model="carInfo.licenseCity" placement="top" placeholder="请选择" :disabled="!isCheck">
                            <Option v-for="(item,index) in cities" style="width: 150px" :label="item.label" :value="item.value" :key="index">{{ item.label }}</Option>
                        </Select>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="carPurpose" label="购车目的">
                        <Select v-model="carInfo.carPurpose" disabled placeholder="请选择">
                            <Option v-for="item in dataDic.carPurpose" :label="item.title" :value="item.value" :key="item.value">{{ item.title }}</Option>
                        </Select>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem prop="loanAmtMax" label="理论最大贷款额">
                        <Input type="text" v-model="carInfo.loanAmtMax" disabled placeholder="请输入">
                        </Input>
                        <Button style="margin-left:5px" ghost type="success" @click="claculateLoanAmtMax" icon="md-calculator" v-show="isCheck">
                        </Button>
                    </FormItem>
                </Col>
            </Row>
            <Row v-show="carInfo.priceWarn!=null">
                <Col span="8">
                    <FormItem></FormItem>
                </Col>
                <Col span="8">
                    <FormItem></FormItem>
                </Col>
                <Col span="8">
                    <FormItem  v-show="carInfo.priceWarn!=null" prop="firmPrice" label="我司车辆预警值" >
                           <span style="color: red" >{{carInfo.priceWarn}}
                            </span>
                    </FormItem>
                </Col>
            </Row>
        </Form>
    </div>
</template>

<script>
    import {
        getByTypes
    } from "_p/basic/api/admin/datadic";
    import {
        getAddressList
    } from "../../../../api/afs-case/infomationDetail/applyCustomerDetail";
    import {
        queryCarDetailInfo,
        claculateLoanAmtMax,
        saveNewCarInfo
    } from "@/projects/afs-core-business/api/afs-case/infomationDetail/carDetail";
    export default {
        name: "new-car-detail",
        components: {
        },
        props: {
            applyNo: {
                type: String,
                required: true
            },
            isCheck: {
                type: Boolean
            }
        },
        data:function () {
            return {
                applyNumber:this.applyNo,
                hasAffix :false,
                provinces:{},
                cities: {},
                provinceLevel: "1",
                dicKeys: [
                    "carFunction","carPurpose","carBodyClass"
                ],
                dataDic:{},
                carInfo:{
                    warningInfo:{
                        car:{
                            showflag:false,
                            title:"",
                            msgs:[]
                        }

                    }
                },
            }
        },
        methods:{
            showCarStyleDetailInfo(){
                this.$emit('function',this.carInfo.carId);
            },
            updateTotalLoanAmtInfo(loanAmtMax){
                this.$emit('maxLoan',loanAmtMax);
            },

            initDataDic(){
                let self = this;
                getByTypes(this.dicKeys).then(res => {
                    if (res.code === "0000" && res.data) {
                        self.dataDic = res.data;
                    }
                });
            },
            initInfo(){
                let params = {
                    applyNo: this.applyNumber
                }
                queryCarDetailInfo(params).then(res => {
                    if (res.code === "0000") {
                        this.carInfo = res.data;
                        this.queryCity(this.carInfo.licenseProvince)
                    }
                });
            },
            //查省份
            initQueryProvince() {
                let params = {
                    level: this.provinceLevel
                }
                getAddressList(params).then(res => {
                    if (res.code === "0000") {
                        this.provinces = res.data;
                    }
                });
            },
            //更新车辆信息
            saveNewCarInfo(){
                if (this.carInfo.licenseCity ==="" || this.carInfo.licenseCity === undefined) {
                    this.$Message.error("请输入上牌地点")
                    return;
                }
                if (this.carInfo.firmPrice ==="" || this.carInfo.firmPrice === undefined) {
                    this.$Message.error("请输入认定价格")
                    return;
                }
                this.$Modal.confirm({
                    title: "保存车辆信息",
                    content: "确定保存当前车辆信息?",
                    onOk: () => {
                        let params = {
                            carId: this.carInfo.id,
                            firmPrice: this.carInfo.firmPrice,
                            licenseProvince: this.carInfo.licenseProvince,
                            licenseCity: this.carInfo.licenseCity
                        }
                        saveNewCarInfo(params).then(res =>{
                            if (res.code === "0000") {
                                this.$Message.success("保存成功");
                                this.initInfo();
                            }else {
                                this.$Message.error("保存失败");
                            }
                        });
                    }
                });
            },
            //查城市
            queryCity(val){
                let params = {
                    upperCode: val
                }
                getAddressList(params).then(res => {
                    if (res.code === "0000") {
                        this.cities = res.data;
                    }
                });
            },
            claculateLoanAmtMax(){
                if(this.carInfo.firmPrice===0 || typeof (this.carInfo.firmPrice)==='undefined' || this.carInfo.firmPrice===""){
                  this.$Message.warning("请输入认定价格！");
                  return true;
                }
                let params = {
                    applyNo: this.applyNumber,
                    carId:this.carInfo.carId,
                    firmPrice: this.carInfo.firmPrice

                }
                this.$Spin.show();
                claculateLoanAmtMax(params).then(res => {
                    this.$Spin.hide();
                    if (res.code === "0000") {
                        this.carInfo.loanAmtMax = res.data.loanAmtMax;
                        this.updateTotalLoanAmtInfo(res.data.loanAmtMax);
                        setTimeout(()=>{
                            this.$emit("isChange")
                        },500);
                    }
                });
            },
            init() {
                this.initDataDic();
                this.initInfo();
                this.initQueryProvince();
            },
        },
        mounted() {
            this.init();
        }
    }
</script>

<style scoped>
 /*   .ivu-collapse-content {
        color:red;
    }*/
 .ivu-input-wrapper {
     display: inline-block;
     width: 150px;
     position: relative;
     vertical-align: middle;
     line-height: normal;
     margin-bottom: 7px;
     margin-left: 2px;
 }
</style>
